home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / csim / source.lha / source / Threads / GnuThreads / producer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-14  |  1.7 KB  |  72 lines

  1. /*
  2.  * producer.c -- an example program to demonstrate lightweight
  3.  * processes, semaphores and time.
  4.  * Copyright (C) 1991 Stephen Crane.
  5.  *
  6.  * This is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 1, or (at your option)
  9.  * any later version.
  10.  *
  11.  * This software is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * see the file COPYING.  If not, write to
  18.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  * author: Stephen Crane, (jsc@doc.ic.ac.uk), Department of Computing,
  21.  * Imperial College of Science, Technology and Medicine, 180 Queen's
  22.  * Gate, London SW7 2BZ, England.
  23.  */
  24.  
  25. #include <string.h>
  26. #include <stdio.h>
  27. #include "gnulwp.h"
  28.  
  29. struct sem *empty, *full, *term;
  30. char c;
  31.  
  32. void f (int n, char** p1, void* dummy3 = 0)
  33. {
  34.         char* p = *p1;
  35.  
  36.     while (n--) {
  37.         waits (empty);
  38.         c = *p++;
  39.         signals (full);
  40.         delayp (1000000);
  41.     }
  42.     signals (term);
  43.     suicidep ();
  44. }
  45.  
  46. void g (int n, char** dummy2 = 0, void* dummy3 = 0)
  47. {
  48.     while (n--) {
  49.         waits (full);
  50.         putchar (c);
  51.         fflush (stdout);
  52.         signals (empty);
  53.     }
  54.     signals (term);
  55.     suicidep ();
  56. }
  57.  
  58. main ()
  59. {
  60.     char *str = "Hello world\n";
  61.     int n = strlen (str);
  62.  
  63.     initlp (1);
  64.     empty = creats (1);
  65.     full = creats (0);
  66.     term = creats (0);
  67.     creatp (2, g, 2048, n, 0, 0);
  68.     creatp (2, f, 2048, n, &str, 0);
  69.     for (n=2; n--; )
  70.         waits (term);
  71. }
  72.